var content = document.querySelector('.content');
var tbody = document.querySelector(".grade-table tbody");
var searchBtn = document.getElementById('searchBtn');
var addButton = document.getElementById("addButton");
var editButton = document.getElementById("editButton");
var deleteButton = document.getElementById("deleteButton");

addButton.addEventListener("click", function() {
  // 添加按钮的点击事件处理逻辑
  window.location.href="order_plan.html";
});

editButton.addEventListener("click", function() {
  var input = prompt("请输入需要修改的信息序号：");
  var searchInput = document.getElementById("search");
  var searchText = searchInput.value;

  // 判断输入是否为空
  if (input !== null && input.trim() !== "") {
    // 将输入的序号转换为数字
    var index = parseInt(input);
    // 判断序号是否有效
    if (!isNaN(index)) {
      // 提示用户输入列名
      var columnName = prompt("请输入列名：");
      // 判断输入是否为空
      if (columnName !== null && columnName.trim() !== "") {
        // 判断列名是否为指定的信息之一
        var dataBaseUse = null;
        if (columnName === "计划课程") {
            dataBaseUse = "Cou_name";
        } else if (columnName === "课程描述") {
            dataBaseUse = "Cou_content";
        } else if (columnName === "上课地点") {
            dataBaseUse = "Cou_location";
        } else if (columnName === "讲师") {
            dataBaseUse = "Cou_teacher";
        } else if (columnName === "价格") {
            dataBaseUse = "Cou_payment";
        } else if (columnName === "公司名称") {
            dataBaseUse = "Com_name";
        } else if (columnName === "公司电话") {
            dataBaseUse = "Com_phone";
        } else {
          dataBaseUse = null;
        }
        //alert(dataBaseUse);
        if (dataBaseUse == null) {
            alert("请输入有效的列名");
        } else {
            //先接受改后的信息
          var change = prompt("请输入修改后的信息：");
            // 判断输入是否为空
          if (change !== null && change.trim() !== "") {
              var confirmation = confirm("确定要修改吗？");
              if (confirmation) {
                //alert(change);
                //ajax调用
                $.ajax({
                      url: '/management/managerservlet/mngChangePlans',             //url方法接口：修改信息，课程
                      method: 'POST',
                      data: {
                      searchText: searchText,
                        i: index,
                        column: dataBaseUse,
                        change: change
                      },
                      success: function(response) {
                        console.log(response);
                        console.log(response.data);
                        alert("修改成功！");
                        window.location.href = "mnglogin_plan.html";
                      }
                });
              } else {
                    alert("取消修改！");
              }
          } else {
              alert("请输入有效的信息！");
          }
        }
      }
      else {
        alert("请输入有效的列名");
      }
    } else {
      alert("请输入有效的序号！");
    }
  }
});

deleteButton.addEventListener("click", function() {
  var input = prompt("请输入需要删除的信息序号：");
  var searchInput = document.getElementById("search");
  var searchText = searchInput.value;

  // 判断输入是否为空
  if (input !== null && input.trim() !== "") {
    // 将输入的序号转换为数字
    var index = parseInt(input);
    // 判断序号是否有效
    if (!isNaN(index)) {
              var confirmation = confirm("确定要删除吗？");
              if (confirmation) {
                //ajax调用
                $.ajax({
                      url: '/management/managerservlet/mngDeletePlans',             //url方法接口：删除信息，课程
                      method: 'POST',
                      data: {
                        searchText: searchText,
                        i: index
                      },
                      success: function(response) {
                        console.log(response);
                        console.log(response.data);
                        alert("删除成功！");
                        window.location.href = "mnglogin_plan.html";
                      }
                });
              } else {
                    alert("取消删除！");
              }
    } else {
      alert("请输入有效的序号！");
    }
  }
});

content.addEventListener('mouseenter', function() {
  content.style.transition = 'background-color 0.3s';
  content.style.backgroundColor = 'rgba(100,100,100,0.5)'; // 将透明度更改为 0.5，使背景色加深
});

content.addEventListener('mouseleave', function() {
  content.style.transition = 'background-color 0.3s';
  content.style.backgroundColor = 'rgba(100,100,100,0.2)'; // 将透明度恢复为 0.2，使背景色变浅
});

var data = [];

$(document).keydown(function(event) {
  if (event.keyCode == 13) { // 13代表回车键的键码
    $('#searchBtn').click();
  }
});

document.addEventListener("DOMContentLoaded", function() {

  $.ajax({
      url: '/management/managerservlet/mngCheckPlans',                              //url方法接口：查询培训
      method: 'POST',
      data: {
        username: "1"
      },
      success: function(response) {
        console.log(response);
        console.log(response.data);
        data = response.data;
        // 将数据数组赋值给相应的元素
        console.log(data.length);

            var currentIndex = 1;
            data.forEach(function(item) {
              var row = document.createElement("tr");
              row.innerHTML = `
                <td>${currentIndex}</td>
                <td>${item.Cou_name}</td>
                <td>${item.Cou_content}</td>
                <td>${item.Cou_location}</td>
                <td>${item.Cou_teacher}</td>
                <td>${item.Cou_payment}</td>
                <td>${item.Com_name}</td>
                <td>${item.Com_phone}</td>
                <td>${item.Cou_pass}</td>
              `;
              tbody.appendChild(row);
              currentIndex++;
            });
      }
    });

  var links = document.querySelectorAll('.sidebar ul li a');

  for (var i = 0; i < links.length; i++) {
    links[i].addEventListener('mouseenter', function() {
      this.style.transition = '0.2s';
      this.style.backgroundColor = '#679';
      this.style.borderRadius = '10px'; // 设置边缘的圆角半径为10px
    });

    links[i].addEventListener('mouseleave', function() {
      this.style.transition = '0.2s';
      this.style.backgroundColor = '';
      this.style.borderRadius = '10px'; // 鼠标移出时恢复原始边缘
    });
  }
});

searchBtn.addEventListener('click', function() {
      var searchInput = document.getElementById("search");
      var searchText = searchInput.value;
      $.ajax({
             url: '/management/managerservlet/mngSearchPlans',                         //url方法接口：搜索，培训
             method: 'POST',
             data: {
                  searchText: searchText
             },
             success: function(response) {
                console.log(response);
                var search = response.data;
                console.log(search);
                if(search.length == 0){
                    alert("没有符合要求的学生！");
                }
                else{
                tbody.innerHTML = "";
                currentIndex = 1;
                search.forEach(function(item) {
                          var row = document.createElement("tr");
                          row.innerHTML = `
                            <td>${currentIndex}</td>
                            <td>${item.Cou_name}</td>
                            <td>${item.Cou_content}</td>
                            <td>${item.Cou_location}</td>
                            <td>${item.Cou_teacher}</td>
                            <td>${item.Cou_payment}</td>
                            <td>${item.Com_name}</td>
                            <td>${item.Com_phone}</td>
                            <td>${item.Cou_pass}</td>
                          `;
                          tbody.appendChild(row);
                          currentIndex++;
                        });
                }
             }
      });
});